package me.lubinn.Incantatio;

import java.io.*;
import java.util.Properties;
import org.bukkit.entity.Player;
import org.bukkit.event.player.*;
import org.bukkit.util.config.Configuration;

// Referenced classes of package me.orion.Incantatio:
//            IncantatioSpell, Incantatio

public class IncantatioPlayerListener extends PlayerListener
{

    public static Incantatio plugin;
    PlayerChatEvent ev;

    public IncantatioPlayerListener(Incantatio instance)
    {
        plugin = instance;
    }

    public void onPlayerMove(PlayerMoveEvent playermoveevent)
    {
    }

    public void onPlayerChat(PlayerChatEvent event)
    {
        IncantatioSpell spellThread = new IncantatioSpell(this);
        String message = event.getMessage();
        spellThread.ev = event;
        if(message.trim().equalsIgnoreCase("tueri") && !spellThread.CheckPermissions("tueri", null))
        {
            message = "tuerifail";
        }
        if(message.trim().equalsIgnoreCase("itero") && spellThread.CheckPermissions("itero", null))
        {
            try
            {
                FileInputStream in = new FileInputStream(Incantatio.IncantatioLog);
                Incantatio.prop.load(in);
                String LastSpell = Incantatio.prop.getProperty(event.getPlayer().getName());
                message = LastSpell;
                in.close();
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
        } else
        {
            try
            {
                FileOutputStream out = new FileOutputStream(Incantatio.IncantatioLog);
                Incantatio.prop.put(event.getPlayer().getName(), message);
                Incantatio.prop.store(out, null);
                out.flush();
                out.close();
            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }
        String components[] = message.trim().split(" ");
        ev = event;
        int m = 0;
        if(components[m].equalsIgnoreCase("magna") || components[m].equalsIgnoreCase("parva"))
        {
            m++;
        }
        if(components.length > m + 1 && components[m + 1].equalsIgnoreCase("augere") && components[m + 2].equalsIgnoreCase("silentium") && Incantatio.config.getBoolean((new StringBuilder(String.valueOf(components[m].toLowerCase()))).append("_enabled").toString(), false))
        {
            ev.setCancelled(true);
        }
        if(components.length > m + 2 && components[m + 2].equalsIgnoreCase("augere") && components[m + 3].equalsIgnoreCase("silentium"))
        {
            if(Incantatio.config.getBoolean((new StringBuilder(String.valueOf(components[m].toLowerCase()))).append("_").append(components[m + 1].toLowerCase()).append("_enabled").toString(), false))
            {
                ev.setCancelled(true);
            } else
            if(Incantatio.config.getBoolean((new StringBuilder(String.valueOf(components[m].toLowerCase()))).append("_").append(components[m + 1].toLowerCase()).toString(), false))
            {
                ev.setCancelled(true);
            }
        }
        spellThread.CastSpell(components, ev);
    }
}
